package com.tank.ui;

import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.scene.SubScene;
import com.tank.TankApp;
import com.tank.constant.Config;
import javafx.animation.PauseTransition;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.util.Duration;

public class SucceedScene extends SubScene {

    private final StackPane pane;
    private final PauseTransition pt;
    private final Text text;

    public SucceedScene() {
        text = new Text("You pass this level");
        int level = FXGL.geti(Config.MAP_LEVEL);
        if (level >= Config.MAX_MAP_LEVEL) {
            text.setText("Mission complete! All over!");
        }
        text.setFill(Color.ORANGE);
        text.setFont(Font.font(35));
        pane = new StackPane(text);
        pane.setPrefSize(FXGL.getAppWidth(), FXGL.getAppHeight());
        pane.setStyle("-fx-background-color: gray");
        pt = new PauseTransition(Duration.seconds(2));
        pt.setOnFinished(event -> {
            FXGL.getSceneService().popSubScene();
            if (level >= Config.MAX_MAP_LEVEL) {
                FXGL.getNotificationService().pushNotification("Congratulations!");
                FXGL.getGameController().gotoMainMenu();
            } else {
                FXGL.inc(Config.MAP_LEVEL, 1);
                FXGL.<TankApp>getAppCast().startLevel();
            }
        });
        getContentRoot().getChildren().add(pane);
    }

    @Override
    public void onCreate() {
        pt.play();
    }
}
